/**
 * Created by xu on 2015/12/31.
 */

$(document).ready(function(){
    getPipeLine("pipeLineUl");//获取管道列表
    getRoomList();
    $("form[name=queryForm]").validationEngine('attach',{
        promptPosition:'bottomLeft'
    });//初始化查询条件的验证

    $('.tab-li li').each(function(i){
        $(this).on('click',function(){
            $(this).addClass('active')
            $(this).siblings().removeClass('active')
            $('.sj-main .sj-0').eq(i).removeClass('none')
            $('.sj-main .sj-0').eq(i).siblings().addClass('none');
            var tabName=$(this).attr("tabName");
            afterClickTab(tabName);

        });
    });

    //initUploader();

});

function getSegment(ulId,lineId){
    if(isEmpty(ulId)||isEmpty(lineId)){
        return;
    }
    $("#"+ulId).prev("div").find("a").html("全部管段").attr("vid","");
    $("#"+ulId).prev("div").find("input").val("");
    var url=p_path+"/public/dict/getPipeSegmentByLineId.json";
    var data_={"id":lineId};
    $().invoke(
        url,
        data_,
        [function(m,r){

            $("#"+ulId).empty();
            $("#"+ulId).append("<li><a href=\"javascript:void(0);\" vid=\"\">全部管段</a></li>");
            if (isEmpty(r)){return;}
            for (var i in r){
                $("#"+ulId).append("<li><a href=\"javascript:void(0);\" vid=\""+(r[i].id)+"\" >"+(r[i].name)+"</a></li>");
            }
        },function(m,r){
            $("#"+ulId).empty();
            $("#"+ulId).append("<li><a href=\"javascript:void(0);\" vid=\"\">全部管段</a></li>");
            alert(m);
        }]
    )
}

function getPipeLine(ulId){
    if (isEmpty(ulId)){
        return;
    }
    $("input[name=pipeLineId]").val('');
    var url=p_path+"/public/dict/getPipeLineList.json";
    var data_={};
    $().invoke(
        url,
        data_,
        function(m,r){
            if (r==null){return;}
            for (var key in r){
                var a=r[key];
                $("ul[name="+ulId+"]").each(function(i,d){
                    var id=$(this).attr("id");
                    $(this).append("<li ><a onclick=getSegment('"+id+"_segmentUl','"+key+"') href=\"javascript:void(0);\" vid=\""+(key)+"\" >"+(a)+"</a></li>")
                });
            }
        }
    );
}

function getRoomList(){
    $("input[name=rooms]").val('');
    var url=p_path+"/public/dict/getRooms.json";
    var data_={};
    $().invoke(
        url,
        data_,
        function(m,r){
            if (r==null){return;}
            for (var i in r){
                var rec=r[i];
                //$(this).append("<li ><a onclick=href=\"javascript:void(0);\" vid=\""+(key)+"\" >"+(a)+"</a></li>")
                $("ul[name=roomsList]").append("<li ><a onclick=href=\"javascript:void(0);\" vid=\""+(rec["id"])+"\" >"+(rec["siteName"])+"</a></li>")
            }
        }
    );
}


//切换标签
var data_man_tab_Map=new Map();
function initMap(){
    data_man_tab_Map.put("pipeCrossOver",afterClickCrossTab);
    data_man_tab_Map.put("identification",afterClickIdentificationTab);
    data_man_tab_Map.put("trackPoint",afterClickTrackPointTab);
    data_man_tab_Map.put("piggingstructure",afterClickPiggingstructure);
}
function afterClickTab(tabName){
    if (tabName==null){return;}
    localStorage.setItem(_nowPage_tab_CacheName,tabName);
    if (data_man_tab_Map.isEmpty()){
        initMap();
    }
    var f=data_man_tab_Map.get(tabName);
    if (f==null){
        return;
    }
    f();
}

function initUploader(tab){
    var upPath="";
    var title="";
    if (tab=="pipeCross"){
        title="导入穿跨越记录";
        upPath="/pipe/data/cross/pipeCrossExcelImport.json";
    }else if (tab=='identification'){
        title="导入地面标识"
        upPath="/pipe/data/identification/identificationImport.json";
    }else if (tab=='trackPoint'){
        title="导入定标点"
        upPath="/pipe/data/trackPoint/trackPointImport.json";
    }else if (tab=='piggingstructure'){
        title="导入收发球装置"
        upPath="/pipe/data/piggingstructure/piggingstructureImport.json";
    }
    var data_={"upUrl":upPath,"title":title};
    openUploadDialog(data_);
}

/**
 * 打开详情
 * @param emxq
 * @param fun
 */
function opDetail(emxq,fun){
    if (fun==null){
        alert("没有详情参数");
        return;
    }
    var tr=$(emxq).parentsUntil("tbody").eq(2);
    var data=tr.data("trData");
    tr.next("tr").find("td").html(fun(data))
    tr.next("tr").fadeToggle();
    $(emxq).toggleClass("active")

    $("tr[dchildrow_='child'] .show-list .s2").each(function(i,d){
        if ($(d).children().length>0){
            var tem=null;
            for (var i=0;i<10;i++){
                tem=$(d).children().get(0);
                console.log($(tem).html())
                if ($(tem).children().length==0){
                    $(tem).attr("title",$(tem).html());
                    break;
                }
            }
            tem=null;
            return true;
        }

        $(d).attr("title",$(d).html())
    })

}

function refreshUpload_(){
    //initUploader();
}
